import os
import shutil
import time

import cv2
import pytesseract


# 获取目录下所有文件
def get_all_file(r):
    def recur(r, file_list=None):
        if not file_list: file_list = []
        try:
            for filename in os.listdir(r):
                file = os.path.join(r, filename)
                if not os.path.isfile(file):
                    file_list = recur(file, file_list)
                else:
                    file_list.append(file)
        except: pass
        return file_list

    file_list = recur(r)
    for file in file_list:
        print(file)

    # 仅获取当前目录文件
    def get_file(r):
        file_list = []
        for filename in os.listdir(r):
            file = os.path.join(r, filename)
            if os.path.isfile(file): file_list.append(file)
        for file in file_list: print(file)
        return file_list


# 删除.DS_Store文件
def remove_DS_file(r):
    def recur(r, file_list=None):
        if not file_list: file_list = []
        try:
            for filename in os.listdir(r):
                file = os.path.join(r, filename)
                if not os.path.isfile(file):
                    file_list = recur(file, file_list)
                elif filename.startswith("._"):
                    file_list.append(file)
                elif filename.startswith(".DS_Store"):
                    file_list.append(file)
        except:
            pass
        return file_list

    file_list = recur(r)
    print(f"{file_list[0]} will be removed.")
    time.sleep(5)
    for file in file_list:
        os.remove(file)
        print(file)


# 对当前目录下文件创建同名文件夹并移入
def create_folder_move_file(r):
    file_list, folder_list = [], []
    for file in os.listdir(r):
        if os.path.isfile(os.path.join(r, file)):  # os.sep
            file_list.append(file)
            if "." in file:
                folder = "".join(file.split(".")[:-1]).rstrip()
                folder_list.append(folder)

    for folder in folder_list:
        try:
            print(f"create folder: {folder}")
            os.mkdir(os.path.join(r, folder))
        except FileExistsError:
            pass

    for file, folder in zip(file_list, folder_list):
        print(f"move file to folder: {file} >> {folder}")
        full_file = os.path.join(r, file)
        move_folder = os.path.join(r, folder)
        shutil.move(full_file, move_folder)


# 字幕重命名
def rename():
    dir = "xxx/"
    pre = dir + "xxx"
    post = "xxx.xx"
    repre = dir + "xxx"
    repost = "xxx.xx"

    for i in range(1, 99):
        if i < 10: num = "0" + str(i)
        else: num = str(i)
        os.rename(pre + num + post, repre + num + repost)


def ocr():
    img = r"C:\Users\Administrator\Desktop\xxx.png"
    print(pytesseract.image_to_string(cv2.imread(img)))


def transcode():  # 字符对象的str和bytes类型通过encode和decode转换
    text = ""  # str对象默认编码为UTF-8
    text = text.encode('unicode-escape')  # unicode-escape可将UTF-8编码转为原始字符集


if __name__ == "__main__":
    # r = os.getcwd()
    # create_folder_move_file(r)
    remove_DS_file(r"E:\\")



